package com.ossjk.qlh.tgb.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.tgb.entity.OrderInfo;
import com.ossjk.qlh.tgb.service.IOrderDetailsService;
import com.ossjk.qlh.tgb.service.IOrderInfoService;
import com.ossjk.qlh.tgb.vo.OrderDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Copyright  Fri Jan 21 20:07:27 CST 2022 QLH. Tech Ltd. All rights reserved.
 *
 * @Package: com.ossjk.qlh.projects
 * @ClassName: OrderInfoController
 * @Description: OrderInfo-控制器
 * @author: chair
 * @date: 2022-01-21 20:07:27
 */
@Api(tags = "订单信息")
@RestController
@RequestMapping("/tgb/orderInfo")
public class OrderInfoController extends BaseController {

    @Autowired
    private IOrderInfoService iOrderInfoService;
    @Autowired
    private IOrderDetailsService iOrderDetailsService;

    @ApiOperation(value = "列表")
    @RequiresPermissions("00300025")
    @GetMapping("/list")
    public ResponseBean<Page<OrderInfo>> list(
            @ApiParam(value = "订单号") @RequestParam(name = "orderNum", required = false) String orderNum,
            @ApiParam(value = "快递号") @RequestParam(name = "expressNum", required = false) String expressNum,
            @ApiParam(value = "用户名") @RequestParam(name = "userName", required = false) String userName,
            @ApiParam(value = "订单状态") @RequestParam(name = "status", required = true) Integer status,
            Page page, HttpServletRequest request) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(orderNum)) {
            queryWrapper.like("order_num", orderNum);
        }
        if (StrUtil.isNotBlank(expressNum)) {
            queryWrapper.eq("express_num", expressNum);
        }
        if (StrUtil.isNotBlank(userName)) {
            queryWrapper.like("user.name", userName);
        }
        if (status != 0) {
            queryWrapper.eq("status", status);
        }
        if (StrUtil.isNotBlank(getBusinessId(request))) {
            queryWrapper.eq("business_id", getBusinessId(request));
        }
        Page pageVo = iOrderInfoService.getOrderInfoPage(queryWrapper, page);

        return ResponseBean.Success(pageVo);
    }


    @ApiOperation(value = "查看详情")
    @RequiresPermissions("003000250005")
    @GetMapping("/toUpdate")
    public ResponseBean<Map<String, Object>> toUpdate(@ApiParam(value = "id") @RequestParam(name = "id") String id) {
        Map<String, Object> map = iOrderDetailsService.getDetailList(id);
        return ResponseBean.Success(map);
    }


    @ApiOperation(value = "删除")
    @RequiresPermissions("003000250010")
    @DeleteMapping("/remove")
    public ResponseBean remove(@ApiParam(value = "ids") @RequestParam(name = "ids") String[] ids) {
        if (iOrderInfoService.removeByIds(Arrays.asList(ids))) {
            iOrderDetailsService.removeListByOrderIds(ids);
            return ResponseBean.Success();
        }

        return ResponseBean.Fail("删除失败");
    }
}
